package cn.theten52.demo.patterns.design.behavior.interpreter.update3.dict;

import org.junit.Before;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.*;

/**
 * TODO
 *
 * @author wangjin
 * @date 2021/1/7
 */


// Unit Test

public class DictionaryInterpreterFixture {

    enum Color{
        RED,
        YELLOW,
        WHITE,
        BLUE,
    }

    DictionaryStore enumStore;
    DictionaryStore mapStore;
    DictionaryExpression expression;


    @Before
    public void setUp(){
        enumStore = new EnumDictionaryStore().setEnumType(Color.class);
        Map<String,String> data = new HashMap<>();
        data.put("R","RED");
        data.put("Y","YELLOW");
        data.put("W","WHITE");
        data.put("B","BLUE");
        mapStore = new MapDictionaryStore().setData(data);
        expression = new DictionaryExpressionImpl();
    }

    @Test
    public void testEnumDictionaryInterpreter(){
        expression.setStore(enumStore);

        //验证从Key查找Value的解释过程
        Context context = new Context()
            .setOperator(DictionaryOperator.KEY_TO_VALUE)
            .setKey("RED");
        expression.evaluate(context);
        assertEquals(Color.RED,(Color)(context.getValue()));

        //验证从Value查找Key的解释过程
        context.setValue(Color.BLUE)
            .setOperator(DictionaryOperator.VALUE_TO_KEY);
        expression.evaluate(context);
        assertEquals("BLUE",context.getKey());
    }
    @Test
    public void testMapDictionaryInterpreter(){
        expression.setStore(mapStore);
        //验证从Key查找Value的解释过程
        Context context = new Context()
            .setOperator(DictionaryOperator.KEY_TO_VALUE)
            .setKey("R");
        expression.evaluate(context);
        assertEquals("RED",context.getValue());
        //验证从Value查找Key的解释过程
        context.setValue("BLUE")
            .setOperator(DictionaryOperator.VALUE_TO_KEY);
        expression.evaluate(context);
        assertEquals("B",context.getKey());
    }
}